﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WoChu.Infrastructure.Log;
using WoChu.Infrastructure.Net;
using WoChu.Infrastructure.Video;
using WoChu.Infrastructure.Web;

namespace VideoUntity
{
    class Program
    {
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();
            ILog log = new Log4Helper();
            log.LogInfo("VideoUntity", "开始执行");
            
            var isDebug = ConfigSetting.GetConfigValueByName<bool>("IsDebug");
            if (isDebug)
                Console.ReadKey();

            string checkLocation = "A1", startDate = "2016-7-7 1:21:00", endDate = "2016-7-6 1:24:00";
            if (args.Length > 0)
            {
                checkLocation = args[0];
                startDate = args[1].Replace("_", " ");
                endDate = args[2].Replace("_", " ");
            }

            try
            {
                VideoServer server = VideoCheck.GetVideoServer(checkLocation);

                VideoHelper video = new VideoHelper(true, log);
                // vedio.Login(server.Ip, userName: "admin", password: "wocu:12345");
                video.LoginWithConfig(server.Ip);
                Console.WriteLine("login success");
                var list = video.iChannelNum.Where(s => s > 0);
                Console.WriteLine("ip:{0} 通道号:{1}", server.Ip, string.Join(",", list));
                string videoFile = VideoCheck.DownLoad(video, server, DateTime.Parse(startDate), DateTime.Parse(endDate), checkLocation);
                Console.WriteLine("开始下载 工作位：{0} 时间范围:{1}-{2},", checkLocation, startDate, endDate);
                int downPos = 0, lastPost = 0;
                while (true)
                {
                    downPos = video.GetDownloadPosition();
                    if (downPos == 100)
                    {
                        Console.WriteLine("下载完成 文件名：{0}", videoFile);
                        break;
                    }
                    else if (downPos != lastPost)
                    {
                        Console.WriteLine("进度到{0}%", downPos);
                        lastPost = downPos;
                    }
                    Thread.Sleep(300);
                }
                FileInfo fileInfo = new FileInfo(videoFile);
                if (FileHelper.GetFileSize(fileInfo) == 0) //若是0，则服务器视频不存在，可以退出
                {
                    fileInfo.Delete();
                    ShowAndLog(string.Format("服务器上没有此视频 :{0}", videoFile), log);
                    return;
                }
              //  ConvertVideo(videoFile, log);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                log.LogError(ex);
            }
            if (isDebug)
                Console.ReadKey();
            log.LogInfo("VideoUntity", "执行结束");
        }

        static void ConvertVideo(string videoFile, ILog log)
        {            
            string newPath = FileHelper.ChangeFileName(videoFile, "_src");
            string exePath = DirectoryHelper.GetDirNoCreate(@"..\tool\ffmpeg.exe", false);
            string args = string.Format("-i {0} -vcodec libx264 {1}", newPath, videoFile);
            log.LogInfo("转换视频", string.Format("文件名：{0}", videoFile));
            Console.WriteLine("转换视频 文件名：{0}", videoFile);
            ProcessHelper.Excute(exePath, args, log);
            FileHelper.Delete(newPath);     // delete file  XXX_sc  
            ShowAndLog("视频转换成功", log);
        }

        static void ShowAndLog(string msg, ILog log)
        {
            log.LogInfo(msg, "");
            Console.WriteLine(msg);
        }
    }
}
